<?php

if(!defined('IN_AACMS') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

include_once libfile('function/cache');
$purl = 'action=articlecat&&operation=list';

loadcache('articlecat');
$articlecat = $_G['cache']['articlecat'];

if($operation == 'list') {
	
	if(submitcheck('submit')) {
	
		foreach($_POST['name'] as $key=>$value) {
			$setarr = array();
			
			if($articlecat[$key] && $articlecat[$key]['catname'] != $value) {
				$setarr[] = "catname='$value'";
			}
			
			if($articlecat[$key] && $articlecat[$key]['displayorder'] != $_POST['neworder'][$key]) {
				$setarr[] = "displayorder='{$_POST['neworder'][$key]}'";
			}
			
			if($setarr) {
				DB::query('UPDATE '.DB::table('articlecat')." SET ".implode(',',$setarr)." WHERE catid = '$key'");
			}
		}
		
		updatecache('articlecat');
		cpmsg('articlecat_update_succeed', $purl, 'succeed');
	}
	
	if(empty($articlecat) && DB::getOne('SELECT COUNT(*) FROM '.DB::table('articlecat'))) {
		
		updatecache('articlecat');
		$articlecat = $_G['cache']['articlecat'];
	}
	$articlecatstr = '';
	foreach ($articlecat as $key=>$value) {
		if($value['level'] == 0) {
			$articlecatstr .= showcategoryrow($key, 0, '');
		}
	}
	include template('articlecat_list', 'admin');
	
} elseif($operation == 'edit') {
	
	if(submitcheck('editsubmit')) {
		
		if($_GET['catid'] && !$articlecat[$_GET['catid']]) {
			cpmsg('articlecat_not_found', '', 'error');
		} else {
			$catid = intval($_GET['catid']);
		}
		
		DB::update("articlecat", $_POST, 'catid='.$catid);		
		updatecache('articlecat');
		cpmsg('articlecat_edit_succeed', $purl, 'succeed');
		
	} 

	if($_GET['catid'] && !$articlecat[$_GET['catid']]) {
		cpmsg('articlecat_not_found', '', 'error');
	} else {
		$cate = $articlecat[$_GET['catid']];
	}
	
	include template('articlecat_form', 'admin');
	
} elseif($operation == 'add') {
	
	if(submitcheck('addsubmit')) {
		
		if(DB::insert("articlecat", $_POST)) {
			updatecache('articlecat');
			cpmsg('articlecat_add_succeed', $purl, 'succeed');
		} else {
			cpmsg('articlecat_add_error', $purl, 'error');
		}
		
	}

	$cate['displayorder'] = 0;
	$cate['perpage'] = 25;
	$cate['maxpages'] = 1000;
	
	include template('articlecat_form', 'admin');
	
} elseif($operation == 'delete') {
	
	if($_GET['catid'] && !$articlecat[$_GET['catid']]) {
		cpmsg('articlecat_not_found', '', 'error');
	} else {
		$catids = $articlecat[$_GET['catid']]['children'];
		$catids[] = $_GET['catid'];
		$catids = dimplode($catids);
		
		DB::delete('article', "catid IN({$catids})");
		DB::delete('articlecat', "catid IN({$catids})");
		updatecache('articlecat');
		cpmsg('articlecat_delete_succeed', $purl, 'succeed');
	}
	
}


function showcategoryrow($key, $level = 0, $last = '') {
	global $_G;

	$value = $_G['cache']['articlecat'][$key];
	$return = '';

	$value['articles'] = DB::getOne('SELECT articles FROM '.DB::table('articlecat')." WHERE catid='$key'");
	$publish = '';
	if(empty($_G['cache']['articlecat'][$key]['publishclosed'])) {
		$publish = '&nbsp;<a href="portal.php?mod=portalcp&ac=article&catid='.$key.'" target="_blank">'.cplang('articlecat_publish').'</a>';
	}
	if($level == 2) {
		$class = $last ? 'lastchildboard' : 'childboard';
		$return = '<tr class="hover" id="cat'.$value['catid'].'"><td>&nbsp;</td><td class="td25"><input type="text" class="txt" name="neworder['.$value['catid'].']" value="'.$value['displayorder'].'" /></td><td><div class="'.$class.'">'.
		'<input type="text" class="txt" name="name['.$value['catid'].']" value="'.$value['catname'].'" />'.
		'</div>'.
		'</td><td>'.$value['articles'].'</td>'.
		'<td>'.(empty($value['publishclosed']) ? cplang('yes') : cplang('no')).'</td>'.
		'<td>'.(!empty($value['commentclosed']) ? cplang('yes') : cplang('no')).'</td>'.
		'<td>'.(empty($value['closed']) ? cplang('yes') : cplang('no')).'</td>'.
		'<td>
		<a href="'.ADMINSCRIPT.'?action=articlecat&operation=edit&catid='.$value['catid'].'">'.cplang('edit').'</a>&nbsp;
		<a href="'.ADMINSCRIPT.'?action=articlecat&operation=delete&catid='.$value['catid'].'" onclick="return window.confirm(\''.cplang('confirm_delete').'\')">'.cplang('delete').'</a></td></tr>';
	} elseif($level == 1) {
		$return = '<tr class="hover" id="cat'.$value['catid'].'"><td>&nbsp;</td><td class="td25"><input type="text" class="txt" name="neworder['.$value['catid'].']" value="'.$value['displayorder'].'" /></td><td><div class="board">'.
		'<input type="text" class="txt" name="name['.$value['catid'].']" value="'.$value['catname'].'" />'.
		'<a class="addchildboard" href="'.ADMINSCRIPT.'?action=articlecat&operation=add&upid='.$value['catid'].'">'.cplang('articlecat_addthirdcat').'</a></div>'.
		'</td><td>'.$value['articles'].'</td>'.
		'<td>'.(empty($value['publishclosed']) ? cplang('yes') : cplang('no')).'</td>'.
		'<td>'.(!empty($value['commentclosed']) ? cplang('yes') : cplang('no')).'</td>'.
		'<td>'.(empty($value['closed']) ? cplang('yes') : cplang('no')).'</td>'.
		'<td>
		<a href="'.ADMINSCRIPT.'?action=articlecat&operation=edit&catid='.$value['catid'].'">'.cplang('edit').'</a>&nbsp;
		<a href="'.ADMINSCRIPT.'?action=articlecat&operation=delete&catid='.$value['catid'].'" onclick="return window.confirm(\''.cplang('confirm_delete').'\')">'.cplang('delete').'</a></td></tr>';
		for($i=0,$L=count($value['children']); $i<$L; $i++) {
			$return .= showcategoryrow($value['children'][$i], 2, $i==$L-1);
		}
	} else {
		$childrennum = count($_G['cache']['articlecat'][$key]['children']);
		$toggle = $childrennum > 25 ? ' style="display:none"' : '';
		$return = '<tbody><tr class="hover" id="cat'.$value['catid'].'"><td onclick="toggle_group(\'group_'.$value['catid'].'\')"><a id="a_group_'.$value['catid'].'" href="javascript:;">'.($toggle ? '[+]' : '[-]').'</a></td>'
		.'<td class="td25"><input type="text" class="txt" name="neworder['.$value['catid'].']" value="'.$value['displayorder'].'" /></td><td><div class="parentboard">'.
		'<input type="text" class="txt" name="name['.$value['catid'].']" value="'.$value['catname'].'" />'.
		'</div>'.
		'</td><td>'.$value['articles'].'</td>'.
		'<td>'.(empty($value['publishclosed']) ? cplang('yes') : cplang('no')).'</td>'.
		'<td>'.(!empty($value['commentclosed']) ? cplang('yes') : cplang('no')).'</td>'.
		'<td>'.(empty($value['closed']) ? cplang('yes') : cplang('no')).'</td>'.
		'<td>
		<a href="'.ADMINSCRIPT.'?action=articlecat&operation=edit&catid='.$value['catid'].'">'.cplang('edit').'</a>&nbsp;
		<a href="'.ADMINSCRIPT.'?action=articlecat&operation=delete&catid='.$value['catid'].'" onclick="return window.confirm(\''.cplang('confirm_delete').'\')">'.cplang('delete').'</a></td></tr></tbody>
		<tbody id="group_'.$value['catid'].'"'.$toggle.'>';
		for($i=0,$L=count($value['children']); $i<$L; $i++) {
			$return .= showcategoryrow($value['children'][$i], 1, '');
		}
		$return .= '</tdoby><tr><td>&nbsp;</td><td colspan="9"><div class="lastboard"><a class="addtr" href="'.ADMINSCRIPT.'?action=articlecat&operation=add&upid='.$value['catid'].'">'.cplang('articlecat_addsubcat').'</a></td></div>';
	}
	return $return;
}

?>